package com.ffcc.fitness.comment.controller;

import com.ffcc.fitness.comment.pojo.dto.CommentAddParam;
import com.ffcc.fitness.comment.pojo.dto.CommentUpdateParam;
import com.ffcc.fitness.comment.service.CommentService;
import com.ffcc.fitness.card.pojo.vo.CommentVO;
import com.ffcc.fitness.base.response.JsonResult;
import com.ffcc.fitness.base.response.StatusCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论控制器（简化版）
 */
@Slf4j
@RestController
@RequestMapping("/api/comments")
@RequiredArgsConstructor
public class CommentController {
    
    private final CommentService commentService;    /**
     * 添加评论
     */
    @PostMapping
    public JsonResult addComment(@RequestBody CommentAddParam param) {
        try {
            commentService.addComment(param);
            return JsonResult.ok();
        } catch (IllegalArgumentException e) {
            log.warn("添加评论参数错误：{}", e.getMessage());
            return new JsonResult(StatusCode.PARAMETER_ERROR, e.getMessage());
        } catch (Exception e) {
            log.error("添加评论失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "添加评论失败");
        }
    }    /**
     * 查询目标的评论列表
     */
    @GetMapping("/target/{targetId}/{targetType}")
    public JsonResult getCommentsByTarget(
            @PathVariable Long targetId,
            @PathVariable Integer targetType) {
        try {
            List<CommentVO> comments = commentService.getCommentsByTarget(targetId, targetType);
            return JsonResult.ok(comments);
        } catch (IllegalArgumentException e) {
            log.warn("查询评论参数错误：{}", e.getMessage());
            return new JsonResult(StatusCode.PARAMETER_ERROR, e.getMessage());
        } catch (Exception e) {
            log.error("查询评论列表失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询评论列表失败");
        }
    }    /**
     * 查询目标的评论数量
     */
    @GetMapping("/count/{targetId}/{targetType}")
    public JsonResult getCommentCount(
            @PathVariable Long targetId,
            @PathVariable Integer targetType) {
        try {
            Long count = commentService.getCommentCount(targetId, targetType);
            return JsonResult.ok(count);
        } catch (IllegalArgumentException e) {
            log.warn("查询评论数量参数错误：{}", e.getMessage());
            return new JsonResult(StatusCode.PARAMETER_ERROR, e.getMessage());
        } catch (Exception e) {
            log.error("查询评论数量失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询评论数量失败");
        }
    }    /**
     * 更新评论内容
     */
    @PutMapping
    public JsonResult updateComment(@RequestBody CommentUpdateParam param) {
        try {
            commentService.updateComment(param);
            return JsonResult.ok();
        } catch (IllegalArgumentException e) {
            log.warn("更新评论参数错误：{}", e.getMessage());
            return new JsonResult(StatusCode.PARAMETER_ERROR, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("更新评论业务错误：{}", e.getMessage());
            return new JsonResult(StatusCode.OPERATION_FAILED, e.getMessage());
        } catch (Exception e) {
            log.error("更新评论失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "更新评论失败");
        }
    }    /**
     * 删除评论
     */
    @DeleteMapping("/{commentId}/{userId}")
    public JsonResult deleteComment(
            @PathVariable Long commentId,
            @PathVariable Long userId) {
        try {
            commentService.deleteComment(commentId, userId);
            return JsonResult.ok();
        } catch (IllegalArgumentException e) {
            log.warn("删除评论参数错误：{}", e.getMessage());
            return new JsonResult(StatusCode.PARAMETER_ERROR, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("删除评论业务错误：{}", e.getMessage());
            return new JsonResult(StatusCode.OPERATION_FAILED, e.getMessage());
        } catch (Exception e) {
            log.error("删除评论失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "删除评论失败");
        }
    }
}
